package com.module.overview.ui.cardHistoryDetails

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.common.app.data.bean.KeyPre
import com.common.base.app.extras.*
import com.common.base.utils.SpUtilsManagement
import com.common.base.utils.ViewUtils
import com.common.base.utils.ViewUtils.getDrawable
import com.module.ble.utils.AizoSDKUtil
import com.module.ble.utils.DeviceFirmType
import com.module.overview.R
import com.module.overview.databinding.OverviewDialogMeasurementPromptLayoutBinding
import kotlinx.coroutines.*

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/07/16
 *
 * 描述：测量提示弹框
 *
 * 修订历史：
 *
 */
class MeasurementPromptDialog(context: Context) :
    Dialog(context), View.OnClickListener {

    private var binding: OverviewDialogMeasurementPromptLayoutBinding? = null

    /**
     *
     */
    private val mContext: Context

    /**
     * 点击监听
     */
    private var listener: OnMeasurementPromptListener? = null

    var mPhone: String = ""
    var countryCode:String = ""

    /**
     * +
     * 构造方法
     *
     * @param context
     */
    init {
        setDialogTheme()
        setCanceledOnTouchOutside(true)
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = OverviewDialogMeasurementPromptLayoutBinding.inflate(LayoutInflater.from(context))
        binding?.root?.let { setContentView(it) }

        binding?.ckbAgmLL?.setOnClickListener(this)
        binding?.tvOk?.setOnClickListener(this)
    }

    override fun onClick(view: View) {

        // 防止多次点击
        if (ViewUtils.isFastClick()) return

        binding?.apply {
            if (tvOk === view) {
                listener?.onMeasurementPromptClick()
                SpUtilsManagement.putBoolean(KeyPre.KEY_NO_SHOW_MEASUREMENT_PROMPT, ckbPro.isChecked())
            }else if(ckbAgmLL === view){
            }
//            else{
//                dismiss()
//            }
        }

    }

    override fun show() {
        super.show()
        //获取当前Activity所在的窗体
        val dialogWindow = window
        //设置Dialog从窗体底部弹出
        dialogWindow?.setGravity(Gravity.CENTER)
        dialogWindow?.decorView?.setPadding(0, 0, 0, 0)
        val lp = dialogWindow?.attributes
        //设置宽
        lp?.width = WindowManager.LayoutParams.MATCH_PARENT
        //设置高
        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
        dialogWindow?.attributes = lp
        //显示对话框
        dialogWindow?.setGravity(Gravity.CENTER)
        //设置dialog的动画效果
        dialogWindow?.setWindowAnimations(R.style.bottomShowStyle)

        when (AizoSDKUtil.getRingFirm()) {
            DeviceFirmType.HDU_FIRM -> binding?.imgRingType?.setImageDrawable(getDrawable(R.drawable.common_ring_type_1_icon))
            DeviceFirmType.YQ_FIRM -> binding?.imgRingType?.setImageDrawable(getDrawable(R.mipmap.ic_logo_launcher))
            else -> binding?.imgRingType?.setImageDrawable(getDrawable(R.mipmap.ic_logo_launcher))
        }

    }

    /**
     * set dialog theme(设置对话框主题)
     */
    private fun setDialogTheme() {
        requestWindowFeature(Window.FEATURE_NO_TITLE) // android:windowNoTitle
        window!!.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) // android:backgroundDimEnabled默认是true的
        window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) // android:windowBackground
    }

    /**
     * @param listener
     */
    fun setListener(listener: OnMeasurementPromptListener?) {
        this.listener = listener
    }

    /**
     *
     */
    interface OnMeasurementPromptListener {
        fun onMeasurementPromptClick()
    }
}